package group2assignone.ucm.administrator;

import group2assignone.dao.MyDAO;
import group2assignone.func.Authentication;
import group2assignone.model.Manager;
import group2assignone.ucm.UCController;
import group2assignone.ucm.UCStartup;
import group2assignone.view.UITool;
import group2assignone.view.administrator.EditManagerView;

/**
 * Use Case: Save Edit Manager                                              <br>
 * Pre-condition: User has clicked save from the  Edit Manager view.        <br>
 * Post-condition: UC Admin main view is run                                 <br>
 * 1. system retrievs the manager data                                       <br>
 * 2. system checks the fields are valid                                    <br>
 * 3. system closes the edit manager view<br>
 * 4. system writes the new data to the database                            <br>
 * 5. system runs Use case admin main view                                  <br>
 *      2. a) Invalid manager data entered                                  <br>
 *              - Display error                                             <br>
 * @author group2                                                           <br>
 */
public class UCSaveEditManager implements UCController {

    private EditManagerView view;
    private Manager model;
    private String oldUsername;
    private Authentication auth;
    private boolean authOk;

    public UCSaveEditManager(Manager m, EditManagerView v) {

        view = v;
        model = m;
        auth = new Authentication();

    }

    public void run() {
        System.out.println("Save");//testing comment
        System.out.println(view.getFirstName());//testing comment

        //1. system retrievs the manager data
        oldUsername = model.getUsername();
        view.upLoad(model);
        //2. system checks the fields are valid
        authOk = auth.checkManagerFields(model);
        if (authOk) {
            //3. system closes the edit manager view
            view.dispose();
            //4. system writes the new data to the database
            MyDAO coms = UCStartup.getInstance().getDAO();
            coms.sQLEditManager(model, oldUsername);

            //5. system runs Use case admin main view
            UCUIAdminMainView uc = UCUIAdminMainView.getInstance();
            uc.run();
        } else {

            //2. a) Invalid manager data entered
            //        - Display error
            UITool.displayError(auth.getErrorMsg(), view);
        }
    }
}

